package com.zly.mybatisdemo.controller;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mysql.cj.util.StringUtils;
import com.zly.mybatisdemo.model.User;
import com.zly.mybatisdemo.service.UserService;
//import com.zly.mybatisdemo.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import com.zly.mybatisdemo.token.NoneAuth;
import com.alibaba.fastjson.JSON;

@RestController
@RequestMapping("User")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/AllUser")
    public List<User> findAllUser() {
        return userService.findAllUser();
    }

    @GetMapping("AddUser")
    public boolean addUser(String playerName, String sex, String phoneNo, String email )
    {
        if (StringUtils.isNullOrEmpty(playerName)||StringUtils.isNullOrEmpty(sex)||StringUtils.isNullOrEmpty(phoneNo)||StringUtils.isNullOrEmpty(email))
            return false;

        User user = new User();
        user.setPlayer_name(playerName);
        user.setSex(sex);
        user.setPhone_no(phoneNo);
        user.setEmail(email);


        return userService.addUser(user)>0;
    }

    @NoneAuth
    @PostMapping("login")
    public String login(String userName, String password)
    {
        if (StringUtils.isNullOrEmpty(userName)||StringUtils.isNullOrEmpty(password))
        {
            return null;
        }

        User user = new User();
        user.setPlayer_name(userName);
        user.setPassword(password);



//        TokenUtils tokenUtils = new TokenUtils();
//        String token = tokenUtils.getToken(userName,password);

        return JSON.toJSONString(userService.login(user)>0, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
    }
}
